package de.saar.penguin.give2.mapviewer;

import de.saar.penguin.give2.mapviewer.notification.Drawable;
import de.saar.penguin.give2.mapviewer.notification.NotificationPlugin;

import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.geom.Rectangle2D;

/**
 * Created by IntelliJ IDEA.
 * User: koller
 * Date: Oct 19, 2009
 * Time: 11:23:35 AM
 * To change this template use File | Settings | File Templates.
 */
public class UpdateNotificationPlugin extends NotificationPlugin {
    private static final int UPDATE_LIFESPAN = 1000;
    private static final int HELP_LIFESPAN = 3000;

    private static final int RADIUS = 50;
    private static final Stroke STROKE = new BasicStroke(2);
    private static final Font HELP_FONT = new Font("Helvetica", Font.BOLD, 72);
    private static final Color HELP_ANNOTATION_BACKGROUND_COLOR = new Color(1.0f, 1.0f, 1.0f, 0.8f);

    public UpdateNotificationPlugin(JMapViewer viewer) {
        super(viewer);
    }

    public void addUpdateAnnotation(String objectName) {
        Rectangle2D boundingBox = viewer.getBoundingBox(objectName);
        final Point annotationCenter = new Point((int) boundingBox.getCenterX(), (int) boundingBox.getCenterY());

        addAnnotation(new Drawable() {
            public void draw(Graphics2D graphics) {
                graphics.setStroke(STROKE);
                graphics.setColor(Color.red);
                graphics.drawOval(annotationCenter.x - RADIUS / 2, annotationCenter.y - RADIUS / 2, RADIUS, RADIUS);
            }
        }, UPDATE_LIFESPAN);
    }

    public void addHelpAnnotation() {
        final int midx = viewer.getWidth() / 2, midy = viewer.getHeight() / 2;
        final String helpString = "?";

        Toolkit.getDefaultToolkit().beep();

        addAnnotation(new Drawable() {
            public void draw(Graphics2D graphics) {
                FontRenderContext frc = graphics.getFontRenderContext();

                graphics.setFont(HELP_FONT);

                float sw = (float) HELP_FONT.getStringBounds(helpString, frc).getWidth();
                LineMetrics lm = HELP_FONT.getLineMetrics(helpString, frc);
                float sh = lm.getAscent() + lm.getDescent();

                int x1 = midx - (int) sw / 2;
                int y1 = midy - (int) sh / 2;
                int w = (int) Math.max(sw * 1.2f, sh * 1.2f);
                int h = w;

                graphics.setColor(HELP_ANNOTATION_BACKGROUND_COLOR);
                graphics.fillRect(x1, y1, w, h);

                graphics.setColor(Color.black);
                graphics.drawRect(x1, y1, w, h);

                float sx = x1 + (w - sw) / 2;
                float sy = y1 + (h + sh) / 2 - lm.getDescent();
                graphics.setColor(Color.red);
                graphics.drawString(helpString, sx, sy);
            }
        },
                HELP_LIFESPAN);
    }
}
